package com.ezops.util;

import java.net.URI;

import microsoft.exchange.webservices.data.EmailMessage;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.ExchangeVersion;
import microsoft.exchange.webservices.data.MessageBody;
import microsoft.exchange.webservices.data.WebCredentials;

import org.springframework.stereotype.Component;

@Component
public class NotificationSender {

//	public static void main(String[] atgs) throws Exception{
//		NotificationSender sender = new NotificationSender();
//		sender.send(new Notification() {
//			String message = "<html> <body><b>Trade Failed !! </b> <p> Trade file failed for  batch Id "
//					+ 1
//					+ " . Message from operations - "
//					+ "Problem with data" + "</body></html>";
//
//			@Override
//			public List<String> getReceipeints() {
//				return Arrays.asList("jeevan@ezops.com");
//			}
//
//			@Override
//			public String getMessage() {
//				// TODO Auto-generated method stub
//				return message;
//			}
//		});
//	}
	public boolean send(Notification notification) throws Exception {

		ExchangeService service = new ExchangeService(
				ExchangeVersion.Exchange2010_SP2);
		ExchangeCredentials credentials = new WebCredentials(
				"alerts@ezops.com", "utellme1@");

		service.setCredentials(credentials);
		service.setUrl(new URI("https://pod51028.outlook.com/ews/exchange.asmx"));
		// service.autodiscoverUrl("vishal@ezops.com",
		// new IAutodiscoverRedirectionUrl() {
		//
		// @Override
		// public boolean autodiscoverRedirectionUrlValidationCallback(
		// String redirectionUrl)
		// throws AutodiscoverLocalException {
		// // The default for the validation callback is to reject
		// // the URL.
		// boolean result = false;
		//
		// URI redirectionUri = null;
		// try {
		// redirectionUri = new URI(redirectionUrl);
		// } catch (URISyntaxException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// return false;
		// }
		//
		// // Validate the contents of the redirection URL. In this
		// // simple validation
		// // callback, the redirection URL is considered valid if
		// // it is using HTTPS
		// // to encrypt the authentication credentials.
		// if (redirectionUri.getScheme() == "https") {
		// result = true;
		// }
		// return result;
		//
		// }
		// });

		service.setTraceEnabled(true);
		EmailMessage msg = new EmailMessage(service);
		
		msg.setSubject("Ezops Trade File Alert !!!");
		msg.setBody(MessageBody
				.getMessageBodyFromText(notification.getMessage()));
		msg.getToRecipients().add(notification.getReceipeints().get(0));
		msg.send();
		return true;

	}

}
